package com.business.web.${pack};

import java.util.List;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import com.business.entity.${pack}.${clazz};
import com.business.service.ServiceException;
import com.business.service.${pack}.${clazz}Service;
import com.business.web.CrudActionSupport;

@Namespace("/${object}")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "${object}.action", type = "redirect") })
public class ${clazz}Action extends CrudActionSupport<${clazz}> {
	
	private static final long serialVersionUID = 1L;
	private ${clazz}Service ${object}Service;

	//--  --//
	private Long id;
	private ${clazz} entity;
	private Page<${clazz}> page = new Page<${clazz}>(10);//


	@Override
	public  String list() throws Exception{
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		//
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = ${object}Service.search${clazz}(page, filters);
		return SUCCESS;
	}

	@Override
	public  String input() throws Exception{
		return INPUT;
	}
	
	@Override
	public  String save() throws Exception{
		${object}Service.save${clazz}(entity);
		return RELOAD;
	}

	@Override
	public  String delete() throws Exception{
		try {
			${object}Service.delete${clazz}(id);
			addActionMessage("delete success");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage("delete failure");
		}
		return RELOAD;
	}

	@Override
	public void prepare() throws Exception {
	}

	/**
	 * 定义在input()前执行二次绑定.
	 */
	public void prepareInput() throws Exception {
		prepareModel();
	}

	/**
	 * 定义在save()前执行二次绑定.
	 */
	public void prepareSave() throws Exception {
		prepareModel();
	}

	/**
	 * 等同于prepare()的内部函数,供prepardMethodName()函数调用. 
	 */
	protected  void prepareModel() throws Exception{
		if (id != null) {
			entity = ${object}Service.get${clazz}(id);
		} else {
			entity = new ${clazz}();
		}
	}


	@Override
	public ${clazz} getModel() {
		// TODO Auto-generated method stub
		return entity;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<${clazz}> getPage() {
		return page;
	}

	public void setPage(Page<${clazz}> page) {
		this.page = page;
	}

	@Autowired
	public void set${clazz}Service(${clazz}Service ${object}Service) {
		this.${object}Service = ${object}Service;
	}


	
	
}
